package com.seaking.service;

import com.seaking.dao.DataDao;
import com.seaking.pojo.Data;
import com.seaking.util.Page;
import com.seaking.util.UUId;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by wchha on 10/24 0024.
 */
@Service
public class DataService {

    private static final String zip = "D:\\workspace-ssm\\dataShare\\src\\main\\webapp\\static\\zip\\";
    private static final String image = "D:\\workspace-ssm\\dataShare\\src\\main\\webapp\\static\\image\\";

    @Autowired
    private DataDao dataDao;

    public Long getCount() {
        Long count = dataDao.getCount();
        return count;
    }

    public List<Data> getAll(Page page) {
        List<Data> all = dataDao.getAll(page);

        if (all.size() > 0) {
            for (Data data : all) {
                String photo = data.getPhoto();
                int a = photo.indexOf(";");
                if (photo != null && a > 0) {
                    data.setPhoto(photo.substring(0, a));
                }
            }
            return all;
        } else return null;
    }

    /**
     * 保存zip文件
     *
     * @param photo
     * @param data
     * @return
     */
    public Data saveZip(MultipartFile[] photo, Data data) {
        String path = zip + getDate();
        String zipName = UUId.random() + ".zip";
        data.setResource(getDate() + zipName);
        ZipOutputStream zipOutputStream = null;
        try {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(new File(path + zipName)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            for (MultipartFile file : photo) {

                InputStream inputStream = file.getInputStream();
                zipOutputStream.putNextEntry(new ZipEntry(file.getOriginalFilename()));
                int emp;
                while ((emp = inputStream.read()) != -1) {
                    zipOutputStream.write(emp);
                }
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOutputStream != null) {
                try {
                    zipOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return data;
    }

    //保存文件
    public Data saveFiles(MultipartFile[] photo, Data data) {
        String path2 = image + getDate();
        StringBuffer stringBuffer = new StringBuffer();
        for (MultipartFile file : photo) {
            //保存图片
            InputStream inputStream = null;
            String name = file.getOriginalFilename();
            String substring = name.substring(name.lastIndexOf("."));
            String fileName = UUId.random() + substring;
            stringBuffer.append(getDate() + fileName + ";");
            try {
                inputStream = file.getInputStream();
                Thumbnails.of(inputStream).size(400, 400).toFile(new File(path2 + fileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (data.getSize() == null) {
                BufferedImage read = null;
                try {
                    read = ImageIO.read(file.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                int height = read.getHeight();
                int width = read.getWidth();
                data.setSize(height + " * " + width);
            }
        }
        int length = stringBuffer.length();
        data.setPhoto(stringBuffer.substring(0, length - 1));
        return data;
    }

    private String getDate() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        String date = instance.get(Calendar.YEAR) + "\\" + instance.get(Calendar.MONTH) + "\\" + instance.get(Calendar.DAY_OF_MONTH) + "\\";
        String path1 = zip + date;
        String path2 = image + date;
        File file1 = new File(path1);
        if (!file1.exists()) file1.mkdirs();
        File file2 = new File(path2);
        if (!file2.exists()) file2.mkdirs();
        return date;
    }

    public void saveData(Data data1) {
        data1.setTime(new java.sql.Date(System.currentTimeMillis()).toString());
        data1.setGood(0);
        data1.setClick(0);
        dataDao.saveData(data1);
    }

    public Data showDetail(Integer id) {
        Data data = dataDao.showDetail(id);
        if (data != null) return data;
        return null;
    }

    public List<String> imageShow(Data data) {
        List<String> images = new ArrayList<String>();
        if (data != null) {
            String photo = data.getPhoto();
            if (photo != null && photo.indexOf(";") > 0) {
                String[] split = photo.split(";");
                for (String image : split) {
                    images.add(image);
                }
            } else if (photo != null && photo.indexOf(";") < 0) {
                images.add(data.getPhoto());
            }
        }
        data.setPhoto(null);
        return images;
    }

    public List<Data> getTitle() {
        List<Data> datas = dataDao.getTitle();
        return datas;
    }

    public void updateGood(Integer id) {
        dataDao.updateGood(id);
    }

    public void updateClick(Data id) {
        id.setClick(id.getGood() + 1);
        dataDao.updateClick(id);
    }

    public List<Data> getChose(Data data, Page page) {
        List<Data> all = dataDao.getChose(data, page);
        if (all.size() > 0) {
            for (Data data1 : all) {
                String photo = data1.getPhoto();
                int a = photo.indexOf(";");
                if (photo != null && a > 0) {
                    data1.setPhoto(photo.substring(0, a));
                }
            }
            return all;
        } else return null;
    }
}
